package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.CertificateQueryDTO;
import com.jeff.tianti.cms.dto.PrizeQueryDTO;
import com.jeff.tianti.cms.entity.Certificate;
import com.jeff.tianti.cms.entity.Prize;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ArticleDaoImpl类
 * @date 2017-03-16
 */

public class PrizeDaoImpl extends CustomBaseSqlDaoImpl implements PrizeCustom {

    public PageModel<Prize> queryPrizePage(PrizeQueryDTO prizeQueryDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select a from Prize a where 1=1 ");
        if (prizeQueryDTO != null) {
            if (StringUtils.isNotBlank(prizeQueryDTO.getTitle())) {
                hql.append(" and a.title like :title ");
                map.put("title", "%"+prizeQueryDTO.getTitle()+"%");
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, prizeQueryDTO.getCurrentPage(), prizeQueryDTO.getPageSize());
    }

    @Override
    public int updateSelectiveById(Prize prize) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update Prize t set");
        if (prize != null) {
            if (!StringUtils.isNotBlank(prize.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(prize.getTitle())) {
                hql.append(" t.title = :title ");
                map.put("title", prize.getTitle());
            }

            if (StringUtils.isNotBlank(prize.getDescription())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.description = :description ");
                } else {
                    hql.append(" t.description = :description ");
                }
                map.put("description", prize.getDescription());
            }
            if (StringUtils.isNotBlank(prize.getImgUrl())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.imgUrl = :imgUrl ");
                } else {
                    hql.append(" t.imgUrl = :imgUrl ");
                }
                map.put("imgUrl", prize.getImgUrl());
            }

            if (StringUtils.isNotBlank(prize.getColumnId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.columnId = :columnId ");
                } else {
                    hql.append(" t.columnId = :columnId ");
                }
                map.put("columnId", prize.getColumnId());
            }
            hql.append("where t.id=:id ");
            map.put("id", prize.getId());
        }
        return this.update(hql.toString(), map);
    }

    public PageModel<Map<String, Object>> queryPrizeList(PageBean pageBean) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,title,DATE_FORMAT(create_time,'%Y-%m-%d') as createTime ,img_url as imgUrl,column_id as  columnId  FROM prize  order by create_time desc ");
        String baseSql = "SELECT count(*) FROM prize ";
        return this.querySqlObjects(hql.toString(), baseSql, map, pageBean.getCurrentPage()+1, pageBean.getPageSize());
    }
}
